package com.yisu.auction.manager.core.service.impl;

import com.yisu.auction.ewf.common.web.EasyUIGrid;
import com.yisu.auction.ewf.entity.SystemConfig;
import com.yisu.auction.manager.core.mapper.SystemConfigMapper;
import com.yisu.auction.manager.core.service.ISystemConfigService;
import com.yisu.auction.manager.domain.request.systemConfigPackage.SystemConfigRequest;
import org.apache.calcite.linq4j.Linq4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * author: 黄萧林
 * Business Description：
 * createTime：2017/8/24
 */
@Service
public class SystemConfigServiceImpl implements ISystemConfigService {

    @Resource
    private SystemConfigMapper systemConfigMapper;

    /**
     * author:黄萧林
     * Business Description:分页获取系统配置信息
     *
     * @param systemConfigRequest
     * @return
     */
    @Override
    public EasyUIGrid<SystemConfig> getSystemConfigInfoByPage(SystemConfigRequest systemConfigRequest) {
        List<SystemConfig> systemConfigs = systemConfigMapper.getSystemConfigInfoByPage(systemConfigRequest);
        EasyUIGrid<SystemConfig> gridData = new EasyUIGrid<>();
        gridData.setTotal(systemConfigRequest.getTotalRecords());
        gridData.setRows(systemConfigs);
        return gridData;
    }

    /**
     * author:黄萧林
     * Business Description:添加/删除系统配置信息
     *
     * @return
     */
    @Override
    public boolean modifySystemConfigInfo(SystemConfig systemConfig) {
        if (StringUtils.isEmpty(systemConfig.getAppSign())) {
            return false;
        }
        List<String> appSigns = Linq4j.asEnumerable(systemConfig.getAppSign().split(",")).toList();
        if (appSigns.size() > 1) {
            systemConfigMapper.deleteByConfigKey(systemConfig.getConfigKey());
            for (String item : appSigns) {
                SystemConfig insertModel = new SystemConfig();
                insertModel.setCreateDate(new Date());
                insertModel.setIsValid(true);
                insertModel.setUpdateDate(new Date());
                insertModel.setAppLoad(systemConfig.getAppLoad());
                insertModel.setDescription(systemConfig.getDescription());
                insertModel.setConfigKey(systemConfig.getConfigKey());
                insertModel.setAppSign(item.trim());
                insertModel.setConfigValue(systemConfig.getConfigValue());
                systemConfigMapper.insertSelective(insertModel);
            }
            return true;
        } else {
            SystemConfig checkModel = systemConfigMapper.selectByPrimaryKey(systemConfig.getId());
            if (checkModel == null) {
                SystemConfig systemConfig2 = new SystemConfig();
                systemConfig2.setCreateDate(new Date());
                systemConfig2.setConfigKey(systemConfig.getConfigKey());
                systemConfig2.setConfigValue(systemConfig.getConfigValue());
                systemConfig2.setDescription(systemConfig.getDescription());
                systemConfig2.setAppLoad(systemConfig.getAppLoad());
                systemConfig2.setAppSign(systemConfig.getAppSign().trim());
                systemConfig2.setIsValid(true);
                return systemConfigMapper.insertSelective(systemConfig2) > 0;
            } else {
                checkModel.setUpdateDate(new Date());
                checkModel.setAppSign(systemConfig.getAppSign().trim());
                checkModel.setAppLoad(systemConfig.getAppLoad());
                checkModel.setDescription(systemConfig.getDescription());
                checkModel.setConfigKey(systemConfig.getConfigKey());
                checkModel.setConfigValue(systemConfig.getConfigValue());
                return systemConfigMapper.updateByPrimaryKeySelective(checkModel) > 0;
            }
        }
    }

    /**
     * author：黄萧林
     * Business Description:删除系统配置信息
     *
     * @return
     */
    @Override
    public boolean deleteSystemConfigInfo(Long id) {
        return systemConfigMapper.deleteByPrimaryKey(id) > 0;
    }

    /**
     * author:黄萧林
     * Business Description:获取编辑的一条数据
     *
     * @param id
     * @return
     */
    @Override
    public SystemConfig getSystemConfig(Long id) {
        return systemConfigMapper.selectByPrimaryKey(id);
    }
}
